#include "pch.h"
#include "com_ittx_alpaca_lib_fortitude_scada_builtin_service_SCADANativeService.h"
#include "SDBClientApi.h"
#include <iostream>

using namespace std;

JNIEXPORT jint JNICALL Java_com_ittx_alpaca_lib_fortitude_scada_builtin_service_SCADANativeService_taskDone
(JNIEnv* env, jobject)
{
	cout << "taskDone" << endl;

	return 0;
}

JNIEXPORT jint JNICALL Java_com_ittx_alpaca_lib_fortitude_scada_builtin_service_SCADANativeService_connectWithDataBase
(JNIEnv* env, jobject, jstring ip, jint port, jint clientId)
{
	const char* pIp = env->GetStringUTFChars(ip, nullptr);

	cout << "connectWithDataBase" << endl;
	cout << "ip:" << pIp << "," << "port:" << (long)port << "," << "clientId:" << (long)clientId << endl;

	return ConnectWithDataBase(pIp, port, clientId);
}

JNIEXPORT void JNICALL Java_com_ittx_alpaca_lib_fortitude_scada_builtin_service_SCADANativeService_disconnectWithDataBase
(JNIEnv* env, jobject)
{
	cout << "disconnectWithDataBase" << endl;

	DisconnectWithDataBase();
}

JNIEXPORT jint JNICALL Java_com_ittx_alpaca_lib_fortitude_scada_builtin_service_SCADANativeService_defineDBSimple
(JNIEnv* env, jobject, jint type, jstring list, jint verb, jint exist)
{
	const char* pList = env->GetStringUTFChars(list, nullptr);

	cout << "defineDBSimple" << endl;
	cout << "type:" << (long)type << "," << "list:" << pList << "," << "verb:" << (long)verb << "," << "exist:" << (long)exist << endl;

	int id = 0;
	DefineDBSimple(type, pList, verb, exist, &id);

	return id;
}

JNIEXPORT jintArray JNICALL Java_com_ittx_alpaca_lib_fortitude_scada_builtin_service_SCADANativeService_selectDBIDInt
(JNIEnv* env, jobject, jint id, jint len)
{
	cout << "selectDBIDInt" << endl;
	cout << "id:" << (long)id << "," << "len:" << (long)len << endl;

	jintArray array = env->NewIntArray(len);
	jint* data = env->GetIntArrayElements(array, nullptr);
	int* iData = new int[len];
	SelectDBIDInt(id, iData, len);
	for (int i = 0; i < len; i++) data[i] = iData[i];

	return array;
}

JNIEXPORT jintArray JNICALL Java_com_ittx_alpaca_lib_fortitude_scada_builtin_service_SCADANativeService_selectDBListIntA
(JNIEnv* env, jobject, jstring list, jint len)
{
	const char* pList = env->GetStringUTFChars(list, nullptr);

	cout << "selectDBListIntA" << endl;
	cout << "list:" << pList << "," << "len:" << (long)len << endl;

	jintArray array = env->NewIntArray(len);
	jint* data = env->GetIntArrayElements(array, nullptr);
	int* iData = new int[len];
	SelectDBListIntA(pList, iData, len);
	for (int i = 0; i < len; i++) data[i] = iData[i];

	return array;
}

JNIEXPORT jdoubleArray JNICALL Java_com_ittx_alpaca_lib_fortitude_scada_builtin_service_SCADANativeService_selectDBIDDouble
(JNIEnv* env, jobject, jint id, jint len)
{
	cout << "selectDBIDDouble" << endl;
	cout << "id:" << (long)id << "," << "len:" << (long)len << endl;

	jdoubleArray array = env->NewDoubleArray(len);
	jdouble* data = env->GetDoubleArrayElements(array, nullptr);
	SelectDBIDDouble(id, data, len);

	return array;
}

JNIEXPORT jdoubleArray JNICALL Java_com_ittx_alpaca_lib_fortitude_scada_builtin_service_SCADANativeService_selectDBListDoubleA
(JNIEnv* env, jobject, jstring list, jint len)
{
	const char* pList = env->GetStringUTFChars(list, nullptr);

	cout << "selectDBListDoubleA" << endl;
	cout << "list:" << pList << "," << "len:" << (long)len << endl;

	jdoubleArray array = env->NewDoubleArray(len);
	jdouble* data = env->GetDoubleArrayElements(array, nullptr);
	SelectDBListDoubleA(pList, data, len);

	return array;
}

JNIEXPORT jint JNICALL Java_com_ittx_alpaca_lib_fortitude_scada_builtin_service_SCADANativeService_updateDBIDInt
(JNIEnv* env, jobject, jint id, jintArray data, jint len)
{
	jint* _data = env->GetIntArrayElements(data, nullptr);
	jsize size = env->GetArrayLength(data);

	cout << "updateDBIDInt";
	cout << "size:" << (long)size << endl;

	int* iData = new int[len];
	for (int i = 0; i < len; i++) iData[i] = _data[i];

	return UpdateDBIDInt(id, iData, len);
}

JNIEXPORT jint JNICALL Java_com_ittx_alpaca_lib_fortitude_scada_builtin_service_SCADANativeService_updateDBListIntA
(JNIEnv* env, jobject, jstring list, jintArray data, jint len)
{
	jint* _data = env->GetIntArrayElements(data, nullptr);
	const char* pList = env->GetStringUTFChars(list, nullptr);
	jsize size = env->GetArrayLength(data);

	cout << "updateDBListIntA";
	cout << "list:" << pList << "," << "size:" << (long)size << endl;

	int* iData = new int[len];
	for (int i = 0; i < len; i++) iData[i] = _data[i];

	return UpdateDBListIntA(pList, iData, len);
}

JNIEXPORT jint JNICALL Java_com_ittx_alpaca_lib_fortitude_scada_builtin_service_SCADANativeService_updateDBIDDouble
(JNIEnv* env, jobject, jint id, jdoubleArray data, jint len)
{
	jdouble* _data = env->GetDoubleArrayElements(data, nullptr);
	jsize size = env->GetArrayLength(data);

	cout << "updateDBIDDouble";
	cout << "size:" << (long)size << endl;

	return UpdateDBIDDouble(id, _data, len);
}

JNIEXPORT jint JNICALL Java_com_ittx_alpaca_lib_fortitude_scada_builtin_service_SCADANativeService_updateDBListDoubleA
(JNIEnv* env, jobject, jstring list, jdoubleArray data, jint len)
{
	jdouble* _data = env->GetDoubleArrayElements(data, nullptr);
	jsize size = env->GetArrayLength(data);

	const char* pList = env->GetStringUTFChars(list, nullptr);

	cout << "updateDBListDoubleA";
	cout << "list:" << pList << "," << "size:" << (long)size << endl;

	return UpdateDBListDoubleA(pList, _data, len);
}
